package edu.whut.mall.admin.controller;

import edu.whut.mall.admin.domain.MerchantParams;
import edu.whut.mall.admin.domain.MerchantSearchParams;
import edu.whut.mall.admin.domain.UserParams;
import edu.whut.mall.admin.service.IMerchantService;
import edu.whut.mall.admin.vo.MerchantVo;
import edu.whut.mall.admin.vo.UserVo;
import edu.whut.mall.common.api.CommonPage;
import edu.whut.mall.common.api.CommonResult;
import edu.whut.mall.common.util.IpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName: RestMerchantController
 * @Description: 商家Controller
 * @Author: lvfanbing
 * @Date: 2020-10-12 17:13
 * @Version: 1.0
 */
@Slf4j
@RequiredArgsConstructor
@RequestMapping("merchant")
@RestController
@Api(value = "商家管理器", tags = {"商家管理器"})
public class RestMerchantController {

    private final IMerchantService merchantService;

    @ApiOperation("商家入驻申请")
    @PostMapping("/merchantEnterApplication")
    public CommonResult merchantEnterApplication(@Valid @RequestBody MerchantParams merchantParams) {
        int result = merchantService.insertOneMerchant(merchantParams);
        if (result <= 0) {
            return CommonResult.failed("提交商家入驻申请失败");
        }
        return CommonResult.success("提交商家申请入驻成功");
    }

    @ApiOperation("删除一个商家")
    @GetMapping("/deleteMerchant")
    public CommonResult deleteMerchant(@RequestParam Long merchantId) {
        int result = merchantService.deleteOneMerchant(merchantId);
        if (result <= 0) {
            return CommonResult.failed("删除商家失败");
        }
        return CommonResult.success("删除商家成功");
    }

    @ApiOperation("更新商家信息")
    @PostMapping("/updateMerchantInfo")
    public CommonResult updateMerchantInfo(@RequestParam Long merchantId,
                                           @RequestBody MerchantParams merchantParams) {
        int result = merchantService.updateOneMerchant(merchantId, merchantParams);
        if (result <= 0) {
            return CommonResult.failed("更新商家信息失败");
        }
        return CommonResult.success("更新商家信息成功");
    }

    @ApiOperation("更新商家的入驻状态")
    @PostMapping("/updateMerchantSettledStatus")
    public CommonResult updateMerchantSettledStatus(@RequestParam Long merchantId,
                                                    @RequestParam Integer status) {
        int result = merchantService.updateOneMerchantSettledStatus(merchantId, status);
        if (result <= 0) {
            return CommonResult.failed("更新商家入驻状态失败");
        }
        return CommonResult.success("更新商家入驻状态成功");
    }

    @ApiOperation("为商家分配管理员")
    @PostMapping("/assignAdminToMerchant")
    public CommonResult assignAdminToMerchant(HttpServletRequest httpServletRequest,
                                              @RequestParam Long merchantId,
                                              @RequestBody UserParams userParams) {

        int result = merchantService.assignAdminToMerchant(merchantId, IpUtil.getIpAddr(httpServletRequest), userParams);
        if (result <= 0) {
            return CommonResult.failed("为商家分配管理员失败");
        }
        return CommonResult.success("为商家分配管理员成功");
    }

    @ApiOperation("设置商家销售款去向")
    @PostMapping("/setProceedsFlowDirection")
    public CommonResult setProceedsFlowDirection(@RequestParam Long merchantId,
                                                 @RequestParam Integer directon) {
        int result = merchantService.setProceedsFlowDirection(merchantId,directon);
        if (result <= 0) {
            return CommonResult.failed("设置商家销售款去向失败");
        }
        return CommonResult.success("设置商家销售款去向成功");
    }

    @ApiOperation("获取所有商家列表")
    @GetMapping("/getAllMerchants")
    public CommonResult getAllMerchants(@RequestParam(value = "pageNum", defaultValue = "1")  Integer pageNum,
                                         @RequestParam(value = "size", defaultValue = "10") Integer size) {
        CommonPage<MerchantVo> merchantPage = merchantService.listAllMerchant(pageNum, size);
        if (merchantPage == null) {
            return CommonResult.failed("获取所有商家列表失败");
        }
        return CommonResult.success(merchantPage, "获取所有商家列表成功");
    }

    @ApiOperation("根据条件获取商家列表")
    @PostMapping("/getMerchantsByCondition")
    public CommonResult getMerchantsByCondition(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "size", defaultValue = "10") Integer size,
                                                @RequestBody MerchantSearchParams merchantSearchParams) {
        CommonPage<MerchantVo> merchantPage = merchantService.listMerchantByCondition(merchantSearchParams, pageNum, size);
        if (merchantPage == null) {
            return CommonResult.failed("获取商家列表失败");
        }
        return CommonResult.success(merchantPage, "获取商家列表成功");
    }

    @ApiOperation("根据商家id获取商家的管理员")
    @GetMapping("/getAdminByMerchantId")
    public CommonResult getAdminByMerchantId(@RequestParam Long merchantId) {
        List<UserVo> userVoList = merchantService.listAllAdminByMerchantId(merchantId);
        if (userVoList == null) {
            return CommonResult.failed("获取商家管理员失败");
        }
        return CommonResult.success(userVoList, "获取商家管理员成功");
    }
}
